package com.zheng.upms.server.controller.manage;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.zheng.common.base.BaseController;
import com.zheng.common.validator.LengthValidator;
import com.zheng.upms.common.constant.UpmsResult;
import com.zheng.upms.common.constant.UpmsResultConstant;
import com.zheng.upms.dao.model.UpmsDict;
import com.zheng.upms.dao.model.UpmsSystemParam;
import com.zheng.upms.dao.model.UpmsSystemParamExample;
import com.zheng.upms.rpc.api.UpmsSystemParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统参数controller
 * Created by shuzheng on 2017/2/6.
 */
@Controller
@Api(value = "系统参数管理", description = "系统参数管理")
@RequestMapping("/manage/systemparam")
public class UpmsSystemParamController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsSystemParamController.class);

    @Autowired
    private UpmsSystemParamService upmsSystemParamService;

    @ApiOperation(value = "系统参数首页")
    @RequiresPermissions("upms:systemparam:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/systemparam/index.jsp";
    }

    @ApiOperation(value = "系统参数列表")
    @RequiresPermissions("upms:systemparam:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        UpmsSystemParamExample upmsSystemParamExample = new UpmsSystemParamExample();
        UpmsSystemParamExample.Criteria criteria = upmsSystemParamExample.createCriteria();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            upmsSystemParamExample.setOrderByClause(sort + " " + order);
        }else{
            upmsSystemParamExample.setOrderByClause(" id asc");
        }
        if (StringUtils.isNotBlank(search)) {
            criteria.andParamTextLike("%" + search + "%");
        }
        List<UpmsSystemParam> rows = upmsSystemParamService.selectByExampleForOffsetPage(upmsSystemParamExample, offset, limit);
        long total = upmsSystemParamService.countByExample(upmsSystemParamExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增系统参数")
    @RequiresPermissions("upms:systemparam:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create() {
        return "/manage/systemparam/create.jsp";
    }

    @ApiOperation(value = "新增系统参数")
    @RequiresPermissions("upms:systemparam:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public Object create(UpmsSystemParam upmsSystemParam) {
        ComplexResult result = FluentValidator.checkAll()
                .on(upmsSystemParam.getParamKey(), new LengthValidator(1, 80, "编码"))
                .on(upmsSystemParam.getParamValue(), new LengthValidator(1, 500, "系统参数值"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        int count = upmsSystemParamService.insertSelective(upmsSystemParam);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除系统参数")
    @RequiresPermissions("upms:systemparam:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = upmsSystemParamService.deleteByPrimaryKeys(ids);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改系统参数")
    @RequiresPermissions("upms:systemparam:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        UpmsSystemParam upmsSystemParam = upmsSystemParamService.selectByPrimaryKey(id);
        modelMap.put("upmsSystemParam", upmsSystemParam);
        return "/manage/systemparam/update.jsp";
    }

    @ApiOperation(value = "修改系统参数")
    @RequiresPermissions("upms:systemparam:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") int id, UpmsSystemParam upmsSystemParam) {;
        ComplexResult result = FluentValidator.checkAll()
                .on(upmsSystemParam.getParamKey(), new LengthValidator(1, 80, "编码"))
                .on(upmsSystemParam.getParamValue(), new LengthValidator(1, 500, "系统参数值"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        int count = upmsSystemParamService.updateByPrimaryKeySelective(upmsSystemParam);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }
    @ApiOperation(value = "刷新数据字典缓存")
    @RequiresPermissions("upms:systemparam:cacherefresh")
    @RequestMapping(value = "/cacherefresh", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Object cacherefresh() {
        int ret = upmsSystemParamService.cacherefresh();
        return new UpmsResult(UpmsResultConstant.SUCCESS, ret);
    }
}
